package com.open.spring.service.impl; 

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.web.util.WebUtils;

import com.open.spring.entity.SysPropertiesBean;

public class SysContextLoaderListener implements ServletContextListener{

	@Override
	public void contextInitialized(ServletContextEvent servletcontextevent) {
		SysPropertiesBean spb = SysPropertiesBean.getInstance();
		
		String rootPath = servletcontextevent.getServletContext().getContextPath();
		if(rootPath.startsWith("/")){
			rootPath = rootPath.substring(1,rootPath.length());
		}
		
		String location = servletcontextevent.getServletContext().getInitParameter("sysProperties");
		
		String wappPath = "";
		
        try {
			wappPath = WebUtils.getRealPath(servletcontextevent.getServletContext(), "");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
        if(wappPath.endsWith(File.separator)){
        	wappPath = wappPath.substring(0, wappPath.length()-1);
        }
        wappPath = wappPath.substring(0, wappPath.lastIndexOf(File.separator));
        String sysPath = wappPath+File.separator+"config"+File.separator+location;
		Map<String,String> map = new HashMap<String, String>();
		map.put("sysPropertiesPath", sysPath);
		map.put("sysProperties", location);
		spb.setSysMap(map);
		
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletcontextevent) {
		// TODO Auto-generated method stub
		
	}

}
